package com.myMinistry.gui;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import com.myMinistry.R;
import com.myMinistry.provider.MinistryContract.LiteratureType;
import com.myMinistry.provider.MinistryService;

public class LiteratureManager extends FragmentActivity {
    private MinistryService database;
    private Spinner literatureTypes;
    private int literatureTypePosition = 0;
	
	@Override
	public void onStop() {
    	super.onStop();
    }
	
	@Override
	public void onStart() {
		super.onStart();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
    		startActivity(new Intent(getApplicationContext(), Summary.class));
			finish();
        }
        return super.onKeyDown(keyCode, event);
    }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	Bundle extras = getIntent().getExtras();
    	setContentView(R.layout.literature);
    	if(extras != null) {
	    	Object[] keys = extras.keySet().toArray();
	    	for(int i = 0; i < keys.length; i++) {
	    		if(keys[i].toString().equals("literatureTypePosition"))
	    			literatureTypePosition = extras.getInt("literatureTypePosition");
	    	}
    	}
    	
	    database = new MinistryService(this);
	    literatureTypes = (Spinner) findViewById(R.id.literatureTypes);
	    
	    database.openReadable();
	    Cursor types = database.fetchTypesOfLiterature();
	    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, types, new String[] {LiteratureType.NAME}, new int[]{android.R.id.text1});
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	literatureTypes.setAdapter(adapter);
    	literatureTypes.setOnItemSelectedListener(
			new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View newview, int newposition, long newID) {
                	updateList((int)newID);
                }
                public void onNothingSelected(AdapterView<?> parent) {
                	// Do nothing.
                }
            }
		);
    	database.close();
    	literatureTypes.setSelection(literatureTypePosition);
    }
	
	public void updatePublisher(int newPublisherID, String publisherName) {
		/** Update display name from fragment */
		ActionBarFragment fragment = (ActionBarFragment)getSupportFragmentManager().findFragmentById(R.id.action_bar_fragment);
		fragment.setSelectedPublisherNameText(publisherName);
	}
	
	public void showDialog() {
		/** DialogFragment.show() will take care of adding the fragment
		 * in a transaction. We also want to remove any currently showing
		 * dialogs, so make our own transaction and take care of that here. */
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
		if(prev != null)
			ft.remove(prev);
		ft.addToBackStack(null);
		/** Create and show the dialog. */
		DialogFrag newFragment = DialogFrag.newInstance(getApplicationContext().getString(R.string.menuPublishers), DialogFrag.ACTIVE_PUBLISHERS);
		newFragment.show(getSupportFragmentManager(), "dialog");
	}
	
	public void createNewPublisherShowDialog() {
		/** DialogFragment.show() will take care of adding the fragment
		 * in a transaction. We also want to remove any currently showing
		 * dialogs, so make our own transaction and take care of that here. */
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
		if(prev != null)
			ft.remove(prev);
		ft.addToBackStack(null);
		/** Create and show the dialog. */
		DialogFrag newFragment = DialogFrag.newInstance(getApplicationContext().getString(R.string.publishersName), DialogFrag.CREATE_PUBLISHER);
		newFragment.show(getSupportFragmentManager(), "dialog");
	}
	
	public void updateList(int _typeID) {
		/** Update display name from fragment */
		LiteratureListFrag fragment = (LiteratureListFrag)getSupportFragmentManager().findFragmentById(R.id.literature_list_fragment);
		fragment.updateList(_typeID);
	}
	
	public void itemTouched(int selectedID) {
		Intent intent = new Intent(this, LiteratureEditor.class);
		intent.putExtra("literatureID", selectedID);
		intent.putExtra("literatureTypePosition", literatureTypes.getSelectedItemPosition());
		startActivity(intent);
		finish();
	}
	
	public void createNewLiterature() {
		Intent intent = new Intent(this, LiteratureEditor.class);
		intent.putExtra("literatureTypePosition", literatureTypes.getSelectedItemPosition());
		startActivity(intent);
		finish();
	}
}